home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / c_dates2.arc / NDATE.C < prev    next >
Text File  |  1991-01-10  |  4KB  |  128 lines

  1. /************************************************************************/
  2. /*  (c)  1987 by James N. Seed,  Atlanta, GA  -  all rights reserved.    */
  3. /*                                    */
  4. /*  This program is provided for example purposes only.  It, and/or    */
  5. /*  its executable equivalent, may not be sold or distributed in any    */
  6. /*  form for profit, without the prior written consent of the author.    */
  7. /************************************************************************/
  8.  
  9. #define     LINT_ARGS
  10.  
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <c_dates.h>
  15.  
  16. static    char errtyp1[] = "\nerror:  cannot interpret the %s in the start date.\n";
  17. static    char errtyp2[] = "\nerror:  start date is not a valid date.\n";
  18.  
  19. static    char *fldnme[3] = { "month", "day", "year" };    /* this is for the */
  20.                             /* error output.   */
  21.  
  22. void _setenvp()    { }
  23.  
  24. void main( argc, argv )
  25.  
  26. int      argc;
  27. char    **argv;
  28.  
  29. {
  30.     unsigned int    gregdte[3], i;        /* m, d, & y ( & index )  */
  31.     unsigned long    julian;            /* julian long start date */
  32.     int        type = 0;        /* type of days to add      */
  33.     char        dtestr[29];        /* to hold verbose dates  */
  34.     char        *nxtfld;        /* for dividing date parm */
  35.     unsigned int    chkdte[3];        /* used to check date     */
  36.  
  37.     if ( argc-- != 3 )            /* two parms input ? */
  38.     {
  39.          printf( "\nNDATE   is a program that calculates a new date that is equal" );
  40.          printf( "\n=====   to the starting date input plus or minus the number of" );
  41.          printf( "\n        days of the specified type.\n" );
  42.          printf( "\nusage:  NDATE [ sdate [+/-]days[A|S|W] ]" );
  43.          printf( "\n\nwhere:" );
  44.          printf( "\n\tsdate is the starting date, and\n" );
  45.          printf( "\n\t[+/-]days is the ( signed ) number of days to be added," );
  46.          printf( "\n\t\t  defined as ONE of the following allowable types:\n" );
  47.          printf( "\n\t\t  A - all days (default)," );
  48.          printf( "\n\t\t  S - all days excluding Sundays," );
  49.          printf( "\n\t\t  W - all days excluding weekends" );
  50.          printf( "\n\n- The starting date MUST be input in the \"month/day/year\" format.\n" );
  51.  
  52.          exit( 0 );
  53.     }
  54.  
  55.     /***********************************************************/
  56.     /* First, convert the input date to the m, d, & y integers */
  57.     /***********************************************************/
  58.  
  59.     for ( i = 0; i < 2; ++i )            /* pick off m & d  */
  60.     {
  61.           if ( gregdte[i] = atoi( argv[1] ) )
  62.           {
  63.            if ( nxtfld = strstr( argv[1], "/" ) )
  64.            {
  65.             argv[1] = ++nxtfld;
  66.            }
  67.            else
  68.            {
  69.             printf( errtyp1, fldnme[++i] );
  70.             exit( 1 );
  71.            }
  72.           }
  73.           else
  74.           {
  75.            printf( errtyp1, fldnme[i] );
  76.            exit( 1 );
  77.           }
  78.     }
  79.  
  80.     gregdte[i] = atoi( argv[1] );            /* then pick off y */
  81.  
  82.     /***********************************************************/
  83.     /*   this line converts the m, d, & y to the JULIAN date   */
  84.     /***********************************************************/
  85.  
  86.     julian = gtoj( gregdte[0], gregdte[1], gregdte[2] );
  87.  
  88.     /*****************************************************/
  89.     /*   these lines check to see if the date is valid   */
  90.     /*****************************************************/
  91.  
  92.     jtog( julian, (char *)chkdte, 0 );
  93.  
  94.     for ( i = 0; i < 3; ++i )
  95.     {
  96.           if ( chkdte[i] != gregdte[i] )
  97.           {
  98.            printf( errtyp2 );
  99.            exit( 1 );
  100.           }
  101.     }
  102.  
  103.     fulldte( julian, dtestr );    /* save verbose to print out later */
  104.  
  105.     /***********************************************/
  106.     /*   these lines compute the new julian date   */
  107.     /***********************************************/
  108.  
  109.     if ( strchr( argv[2], 'S' ) ) type = 1;
  110.     if ( strchr( argv[2], 'W' ) ) type = 2;
  111.  
  112.     julian = newdate( julian, atol( argv[2] ), type );
  113.  
  114.     /******************************************/
  115.     /*   these lines print out the new date   */
  116.     /******************************************/
  117.  
  118.     printf( "\n\t  %li days from %s%s is", atol( argv[2] ), dtestr,
  119.          type ? type == 1 ? ", excluding Sundays,"
  120.                   : ", excluding weekends," : "" );
  121.  
  122.     fulldte( julian, dtestr );
  123.  
  124.     printf( "\n\n\t  %s.\n", dtestr );
  125.  
  126.     exit( 0 );
  127. }
  128.